﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MySql.Data.MySqlClient;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TravellerApp1.Views
{
    /// <summary>
    /// UserLogin.xaml 的交互逻辑
    /// </summary>
    public partial class UserLogin : Window
    {
        public UserLogin()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string uid = tb1.Text;
            string pwd = pb1.Password;
            string name = "";
            string pwd_insql = "";
            try
            {
                string cmd1 = "select * from customers where custID = '" + uid + "';";
                MySqlDataReader reader = MysqlHelper.ExecuteSqlScript(cmd1);
                while (reader.Read())
                {
                    name = reader[0].ToString();
                    pwd_insql = reader[2].ToString();
                }
                if(String.Compare(pwd, pwd_insql) == 0)
                {
                    WinHelper.SetId(uid);
                    WinHelper.SetName(name);
                    MessageBox.Show(name + "，登录成功");
                    GotoUserWindow(this);
                }
                else
                {
                    MessageBox.Show("账号或密码错误");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }

        public static void GotoUserWindow(UserLogin current)
        {
            UserWindow login1 = new();
            login1.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            login1.Show();
            WinHelper.ForceClose(current);
        }
    }
}
